package xyz.xiaomeiqiu;

import java.util.Random;
import java.util.concurrent.*;

public class App {

    public static void main(String[] args) {
        long start=System.currentTimeMillis();
        ExecutorService executor = Executors.newCachedThreadPool();
        Future<Integer> result = executor.submit(new Callable<Integer>() {
            public Integer call() throws Exception {
                return sum(4);//这是得到的返回值
            }
        });
        executor.shutdown();
        try {
            // 拿到result 并输出
            System.out.println("异步计算结果为：" + result.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        System.out.println("使用时间："+ (System.currentTimeMillis()-start) + " ms");

        // join
        int x ;
        Thread tread = new Thread(new Runnable() {
            public void run() {
                sum(19);
            }
        });
        tread.start();
        // 然后退出main线程
    }

    private static int sum(int x) {
        return fibo(x);
    }

    private static int fibo(int a) {
        if ( a < 2)
            return 1;
        return fibo(a-1) + fibo(a-2);
    }
}
